package com.woniuxy.controller;

import com.alibaba.excel.EasyExcel;
import com.github.pagehelper.PageInfo;
import com.woniuxy.common.CarMaintenanceExcelListener;
import com.woniuxy.common.Result;
import com.woniuxy.dto.CarMaintenanceDto;
import com.woniuxy.entity.CarMaintenance;
import com.woniuxy.service.CarMaintenanceService;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/carHealth")
public class CarMaintenanceController {
    @Autowired
    private CarMaintenanceService carMaintenanceService;
    @PostMapping("/findPage")
    @ApiOperation("分页查询")
    @ApiImplicitParam(name = "condition", value = "多条件查询的参数", required = true)
    public Result<PageInfo<CarMaintenanceDto>> findPage(@RequestBody Map<String,Object> condition){
        //int maintenanceState = (int) condition.get("maintenanceState");
        int pageNum = (int) condition.get("pageNum");
        int pageSize = (int) condition.get("pageSize");
        PageInfo<CarMaintenanceDto> pageInfo = carMaintenanceService.findByPage(condition,pageNum, pageSize);
        //返回结果
        return Result.success(pageInfo);
    }

    /**
     * 添加
     */
    @PostMapping("/save")
    public Result<Integer> save(@RequestBody CarMaintenance carMaintenance) {
        int row = carMaintenanceService.save(carMaintenance);
        return Result.success("成功添加保养记录", row);
    }

    /**
     * 修改
     */
    @PostMapping("/update")
    public Result<Integer> update(@RequestBody CarMaintenance carMaintenance) {
        int row = carMaintenanceService.updateById(carMaintenance);
        return Result.success("成功修改车辆保养记录", row);
    }
    /**
     * 删除
     */
    @GetMapping("/deleteById")
    public Result<Integer> deleteById(Integer id){
        int row = carMaintenanceService.deleteById(id);
        return Result.success("成功删除车辆保养记录", row);
    }
    /**
     * 批量删除
     */
    @PostMapping("/deleteBatch")
    public Result<Integer> deleteBatch(@RequestBody String[] ids) {
        int row = carMaintenanceService.deleteBatch(ids);
        return Result.success("批量删除车辆保养记录成功", row);
    }

    /**
     * 导出数据Excel
     */
    @GetMapping("/export")
    public void exportData(HttpServletResponse response) throws IOException {
        //1.调用业务层获取要导出数据
        List<CarMaintenanceDto> list = carMaintenanceService.findAll();
        //2.设置响应头，指定下载文件
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        //文件名不使用汉字，否则需要进行编码，文件名会有乱码。告诉浏览器，这个文件要下载，并且指定文件名
        response.setHeader("content-disposition","attachment;filename=carMaintenance.xlsx");
        //3.调用EasyExcel的方法导出文件
        //获取响应输出流
        ServletOutputStream out = response.getOutputStream();
        //参数：输出流，实体类
        EasyExcel.write(out,CarMaintenanceDto.class)
                .sheet("车辆保养")   //工作簿名字
                .doWrite(list);  //要写出的数据列表
    }
    /**
     * 导入Excel
     */
    @PostMapping("/import")
    public ResponseEntity<String> importData(MultipartFile file) throws IOException {
        //获取输入流
        InputStream inputStream = file.getInputStream();
        //创建Excel的监听器
        CarMaintenanceExcelListener listener = new CarMaintenanceExcelListener(carMaintenanceService);
        //参数：输入流，实体类，监听器对象
        EasyExcel.read(inputStream,CarMaintenanceDto.class,listener).sheet().doRead();
        return ResponseEntity.ok("成功导入" + listener.getRowCount() + "条记录");
    }
}
